package com.product.controller;

import java.util.List;
import java.util.List;
import java.util.Map;

import com.product.entity.AttrEntity;
import com.product.service.AttrAttrgroupRelationService;
import com.product.service.AttrService;
import com.product.service.CategoryService;
import com.product.service.impl.AttrAttrgroupRelationServiceImpl;
import com.product.vo.AttrGroupRelationVo;
import com.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.product.entity.AttrGroupEntity;
import com.product.service.AttrGroupService;
import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.R;

/**
 * 属性分组
 *
 * @author
 * @email 3299686223@qq.com
 * @date 2022-08-08 14:42:26
 */
@RestController
@RequestMapping("/product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    /**
     * 列表
     */
    @GetMapping("/list")
    //@RequiresPermissions(":attrgroup:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{attrGroupId}")
    //@RequiresPermissions(":attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catalogId = attrGroup.getCatelogId();

        List<Long> path = categoryService.findCatelogPath(catalogId);
        attrGroup.setCatelogPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions(":attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions(":attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions(":attrgroup:delete")
    public R delete(@RequestBody List<Long> attrGroupIds) {
        attrGroupService.removeByIds(attrGroupIds);

        return R.ok();
    }

    /**
     * 获取属性分组的关联的所有属性
     *
     * @param attrGroupId 属性分组id
     * @return 结果
     */
    @GetMapping("/attr/relation")
    public R attrRelation(@RequestParam("attrGroupId") Long attrGroupId) {
        List<AttrEntity> data = attrService.getRelationAttr(attrGroupId);
        return R.ok().put("data", data);
    }

    /**
     * 删除属性与分组的关联关系
     *
     * @param vos 关系
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R attrRelationDelete(@RequestBody List<AttrGroupRelationVo> vos) {
        attrService.deleteRelation(vos);
        return R.ok();
    }

    @GetMapping("/noattr/relation")
    public R attrNoRelation(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.getNoRelationAttr(params);
        return R.ok().put("page", page);
    }

    @PostMapping("/attr/relation")
    public R attrRelation(@RequestBody List<AttrGroupRelationVo> vos) {
        relationService.saveBatch(vos);
        return R.ok();
    }
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        //1、查出当前分类下的所有属性分组
        //2、查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data", vos);

    }
}
